<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/14
 * Time: 11:15
 */

namespace Home\Controller;
use Think\Controller;

class ReturnInfoController extends Controller{
    public function showReturn(){
        $userInfo=session('UserInfo');
        if(empty($_POST)&&empty($_GET)){
            session('returnSearch',' ');
            session('returnType',0);
            session('returnTime',1000);
        }
        else if (!empty($_POST)) {//如果搜索框有内容，则提取内容
            if($_POST['returnSearch']==NULL){
                session('returnSearch',' ');//如果内容为空，则设置未空格字符串
            }else {
                session('returnSearch', $_POST['returnSearch']);//如果内容不为空，则设置搜索框中的内容
            }
        } else if (!empty($_GET)) {//时间选择
            if($_GET['returnTime']==NULL && session('returnTime')==NULL){
                session('returnTime',1000);
            }else if($_GET['returnTime']!=NULL){
                session('returnTime',$_GET['returnTime']);//time值赋值为新的get内的值
            }
            if($_GET['returnType']==NULL && session('returnType')==NULL){
                session('returnType',0);
            }else if($_GET['returnType']!=NULL){//如果重新选择拼购类型，则把时间筛选重置为全部
                session('returnType',$_GET['returnType']);
                session('returnTime',1000);
            }
            session('returnSearch',' ');
        }
        $return=D("ReturnInfo");
        $returnController=A("ReturnInfo");
        $search=A('OrderInfo');
        $returnInfo=$return->queryUserAll($userInfo['id']);
        $turn=count($returnInfo);
        if(session('returnType')==1){
            $returnInfo=$returnController->findUncompleted($returnInfo,$turn);
        }else if(session('returnType')==2){
            $returnInfo=$returnController->findCompleted($returnInfo,$turn);
        }
        if(session('returnSearch')!=' ') {//如果session中search
            $returnInfo = $search->orderSearch($returnInfo, session('returnSearch'),$turn);
        }
        $returnInfo=$returnController->returnTimeFilter($returnInfo,session('returnTime'),$turn);
//        var_dump($returnInfo);
        $this->assign('returnInfo',$returnInfo);
        $this->display("User/return");
    }
    public function returnTimeFilter($returnInfo,$time,$turn)//我的拼购时间筛选
    {
//        var_dump($time);

        //$time如果是N个月则为整数N，如果是全部，则为100
        for ($i = 0; $i <= $turn; $i++) {
            $info = $returnInfo[$i];
//            var_dump($info['applytime']);
            $times=date('Y-m-d h:i:s', time() - $time * 30 * 86400);
//            var_dump($times);
            if ($info['applytime'] <$times ) {

                unset($returnInfo[$i]);
            }
        }
        return $returnInfo;
    }
    public function findCompleted($returnInfo,$turn){
        for($i=0;$i<=$turn;$i++){
            $info=$returnInfo[$i];
            if($info['issuccess']==2){//如果存在已经揭晓的拼购，则删除
                unset($returnInfo[$i]);
            }
        }
        return $returnInfo;
    }
    public function findUncompleted($returnInfo,$turn){
        for($i=0;$i<=$turn;$i++){
            $info=$returnInfo[$i];
            if($info['issuccess']==0||$info['issuccess']==1){//如果存在已经揭晓的拼购，则删除
                unset($returnInfo[$i]);
            }
        }
        return $returnInfo;
    }
    public function findOneReturn(){
        //var_dump($_GET);
        $return=D('ReturnInfo');
        $returnInfo=$return->getOne($_GET['detailId']);
        $this->assign('returnInfo',$returnInfo);
        $this->display("User/return");
    }
    public function detailReturn(){
//        var_dump($_GET);
        $this->assign('returnInfo',$_GET);
        $this->display('User/singleReturn');
    }
}